import math
from scipy.optimize import fsolve

import os
import openpyxl
import 龙头xy as long
import 离散点选择 as selectLiSan
import 获得所有碰撞的板 as getCrashBoard

a = 0.55 / (math.pi * 2)


def getNextJiao(x0, y0, len, cjiao):
    p = 0.01
    flen = len
    fx = x0
    fy = y0

    def qaulityJiao(jiao):
        x1 = jiao * a * math.cos(jiao)
        y1 = jiao * a * math.sin(jiao)
        q1 = (y1 - fy) ** 2 + (x1 - fx) ** 2 - flen ** 2
        return q1

    nextJiao = fsolve(qaulityJiao, cjiao + p)
    while nextJiao[0] < cjiao or math.fabs(a * cjiao - a * nextJiao[0]) > 0.55 / 2:
        p = p + 0.1
        nextJiao = fsolve(qaulityJiao, cjiao + p)
    return nextJiao[0]


def getPreJiao(x0, y0, len, cjiao):
    p = 0.01
    flen = len
    fx = x0
    fy = y0

    def qaulityJiao(jiao):
        x1 = jiao * a * math.cos(jiao)
        y1 = jiao * a * math.sin(jiao)
        q1 = (y1 - fy) ** 2 + (x1 - fx) ** 2 - flen ** 2
        return q1

    preJiao = fsolve(qaulityJiao, cjiao - p)
    while preJiao[0] > cjiao or math.fabs(a * cjiao - a * preJiao[0]) > 0.55 / 2:
        p = p + 0.1
        preJiao = fsolve(qaulityJiao, cjiao - p)
    return preJiao[0]


def getAllDots():
    path = r"E:\数学建模\2024A"
    os.chdir(path)  # 修改工作路径

    workbook = openpyxl.load_workbook('result1.xlsx')  # 返回一个workbook数据类型的值
    sheet = workbook['位置']  # 获取指定sheet表

    cells = sheet['KP2:KP449']
    dots = []
    g = 0
    dot = [0, 0]
    for i in cells:
        for j in i:
            dot[g % 2] = j.value
            if g % 2 == 1:
                dots.append([dot[0], dot[1]])
            g += 1
    dots[0][0] = float(format(dots[0][0], '.6f'))
    dots[0][1] = float(format(dots[0][1], '.6f'))
    return dots


def getAllJiaos():
    xx, yy, jiao = long.xyByTime(300)
    jiaos = []
    jiaos.append(jiao)
    for i in range(223):
        if i == 0:
            clen = 2.86
        else:
            clen = 1.65
        jiaos.append(getNextJiao(x0=xx, y0=yy, len=clen, cjiao=jiao))
        jiao = jiaos[len(jiaos) - 1]
        xx = a * jiao * math.cos(jiao)
        yy = a * jiao * math.sin(jiao)
    return jiaos


def getJiaoAndDots():
    dots = getAllDots()
    jiaos = getAllJiaos()

    tempJiaos = []
    tempDots = []
    while int(jiaos[0]) > 0:
        for i in range(len(jiaos)):
            tempJiaos.append(jiaos[i])
            tempDots.append([dots[i][0], dots[i][1]])
        preJiao = getPreJiao(x0=dots[1][0], y0=dots[1][1], len=1.65, cjiao=jiaos[1])
        print(preJiao)
        prex = a * preJiao * math.cos(preJiao)
        prey = a * preJiao * math.sin(preJiao)
        touJiao = getPreJiao(x0=prex, y0=prey, len=2.86, cjiao=preJiao)
        toux = a * touJiao * math.cos(touJiao)
        touy = a * touJiao * math.sin(touJiao)
        for i in range(224 - 3, -1, -1):
            jiaos[i + 2] = jiaos[i + 1]
            dots[i + 2][0] = dots[i + 1][0]
            dots[i + 2][1] = dots[i + 1][1]
        jiaos[0] = touJiao
        jiaos[1] = preJiao
        dots[0][0] = toux
        dots[0][1] = touy
        dots[1][0] = prex
        dots[1][1] = prey
        if int(jiaos[0]) <= 0:
            break
        resultIndexsTou = selectLiSan.getLiSanDots(jiaos, 0)
        crashBoardsTou = getCrashBoard.getCrashBoard(dots, resultIndexsTou, 0)
        resultIndexs1 = selectLiSan.getLiSanDots(jiaos, 1)
        crashBoards1 = getCrashBoard.getCrashBoard(dots, resultIndexs1, 1)
        if len(crashBoardsTou) + len(crashBoards1) > 0:
            break
        tempJiaos = []
        tempDots = []

    v = 1
    # print(format(tempDots[0][0], '.6f'), end='\t')
    # print(format(tempDots[0][1], '.6f'), end='\t')

    for i in range(len(tempJiaos) - 1):
        # print(format(tempDots[i + 1][0], '.6f'), end='\t')
        # print(format(tempDots[i + 1][1], '.6f'), end='\t')
        r1 = tempJiaos[i + 1] * a
        r0 = tempJiaos[i] * a
        v = float(format((v * r1) / r0, '.6f'))
        # print(v)
    return tempJiaos, tempDots
